package cn.lili.controller.feign;

import cn.lili.common.utils.StringUtils;
import cn.lili.modules.order.order.entity.vo.UnionMonthFundsVO;
import cn.lili.modules.order.order.entity.vo.UnionOrderParams;
import cn.lili.modules.promotion.client.GreenClient;
import cn.lili.modules.promotion.entity.dos.GreenConfig;
import cn.lili.modules.promotion.entity.dos.GreenContributionInfo;
import cn.lili.modules.promotion.entity.dto.GreenConfigValueDTO;
import cn.lili.modules.promotion.service.GreenConfigService;
import cn.lili.modules.promotion.service.GreenContributionInfoService;
import cn.lili.modules.promotion.service.GreenUnitPriceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiangqian
 * @version 1.0
 * @data 2023/12/21 15:57
 */
@RestController
@RequiredArgsConstructor
public class GreenController implements GreenClient {

    private final GreenUnitPriceService greenUnitPriceService;
    private final GreenContributionInfoService greenContributionInfoService;
    private final GreenConfigService greenConfigService;
    @Override
    public Boolean initGreenUnitPrice() {
        return greenUnitPriceService.initGreenUnitPrice();
    }

    /**
     * 添加待结算贡献值/绿色积分
     *
     * @param greenContributionInfo 添加数据{@link GreenContributionInfo}
     * @return 返回结果
     */
    @Override
    public Boolean saveSettle(GreenContributionInfo greenContributionInfo) {
        return greenContributionInfoService.saveSettle(greenContributionInfo);
    }

    /**
     * 查询最新功能配置
     */
    @Override
    public GreenConfigValueDTO getNewGreenConfigValueDTO() {
        return greenConfigService.getNewGreenConfigValueDTO();
    }

    /**
     * 添加结算贡献值/绿色积分
     *
     * @param orderSn 子订单ID
     * @return 返回结果
     */
    @Override
    public Boolean settle(String orderSn, String userId) {
        return greenContributionInfoService.settle(orderSn,userId);
    }

    /**
     * 修改状态为直接打款/取消订单
     *
     * @param orderSn 订单号
     * @param userId  用户ID
     * @param status  状态
     */
    @Override
    public Boolean settleDirect(String orderSn, String userId, String status) {
        return greenContributionInfoService.settleDirect(orderSn,userId,status);
    }

    @Override
    public GreenContributionInfo getGreenContributionInfo(GreenContributionInfo greenContributionInfo) {
        LambdaQueryWrapper<GreenContributionInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(greenContributionInfo.getOrderSn()),GreenContributionInfo::getOrderSn,greenContributionInfo.getOrderSn());
        queryWrapper.eq(StringUtils.isNotEmpty(greenContributionInfo.getUserId()),GreenContributionInfo::getUserId,greenContributionInfo.getUserId());
        queryWrapper.eq(StringUtils.isNotEmpty(greenContributionInfo.getType()),GreenContributionInfo::getType,greenContributionInfo.getType());
        queryWrapper.eq(StringUtils.isNotEmpty(greenContributionInfo.getStatus()),GreenContributionInfo::getStatus,greenContributionInfo.getStatus());
        queryWrapper.eq(StringUtils.isNotEmpty(greenContributionInfo.getUserType()),GreenContributionInfo::getUserType,greenContributionInfo.getUserType());
        queryWrapper.last("limit 1");
        return greenContributionInfoService.getOne(queryWrapper);
    }

    @Override
    public Page<UnionMonthFundsVO> getSumContributeSettleByUnion(UnionOrderParams params) {
        return greenContributionInfoService.getSumContributeSettleByUnion(params);
    }

}
